package com.example.springbootuploadanddownload.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springbootuploadanddownload.domain.Files;
import com.example.springbootuploadanddownload.domain.enums.FileTypeEnum;
import com.example.springbootuploadanddownload.mapper.FilesMapper;
import com.example.springbootuploadanddownload.service.FilesService;
import com.example.springbootuploadanddownload.util.FileToMultipartFile;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.util.Objects;

@Service
public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements FilesService {
    @Override
    public HttpServletResponse download(HttpServletResponse response, Integer id) {
        try {
            byte[] buf;
            Files byId = getById(id);
            if (byId == null) {
                return response;
            }
            buf = byId.getFile();
            String suffix = FileToMultipartFile.getSuffix(byId.getName());
            String contentType = "";
            switch (Objects.requireNonNull(FileTypeEnum.getEnum(suffix))) {
                case DOC:
                    contentType = "application/msword";
                    break;
                case DOCX:
                    contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                    break;
                case PDF:
                    contentType = "application/powerpoint";
                    break;
                case JPE:
                case JPG:
                case JPEG:
                    contentType = "image/jpeg";
                    break;
                case PNG:
                    contentType = "image/png";
                    break;
                case ZIP:
                    contentType = "application/zip";
                    break;
                case TAR:
                    contentType = "application/x-tar";
                    break;
                case GZ:
                    contentType = "application/x-gzip";
                    break;
                case XLS:
                    contentType = "application/vnd.ms-excel";
                    break;
                case XLSX:
                    contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                    break;
                case PPT:
                    contentType = "application/vnd.ms-powerpoint";
                    break;
                case PPTX:
                    contentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
                    break;
            }

            // 清空
            response.reset();
            String encodedFileName = URLEncoder.encode(byId.getName().replaceAll(" ", "+"), "UTF-8");
            // 设置header
            response.addHeader("Content-Disposition", "attachment;filename=" + encodedFileName);
//            response.addHeader("Content-Length", "" + byId.getFileNo());
            response.setCharacterEncoding("UTF-8");
            response.setContentType(contentType);
            response.getOutputStream().write(buf);
            // 强制输出，不然会留在内存里丢失掉
            response.getOutputStream().flush();
            return response;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
